package com.example.highclasscomponent;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


public class DynamicFragment extends Fragment {

    public static DynamicFragment newInstance(int position, int image_id, String desc) {
        DynamicFragment fragment = new DynamicFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        args.putInt("image_id", image_id);
        args.putString("desc", desc);

        fragment.setArguments(args);
        return fragment;
    }

    private int getPagerPosition() {
        int position = getArguments().getInt("position");
        return position;
    }
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Log.d("tang", "onAttach: "+getPagerPosition());
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("tang", "onCreate: " + getPagerPosition());
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("tang", "onResume: "+getPagerPosition());
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d("tang", "onPause: "+getPagerPosition());
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d("tang", "onStop: "+getPagerPosition());
    }

    @Override
    public void onDetach() {

        super.onDetach();
        Log.d("tang", "onDetach: " + getPagerPosition());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("tang", "onDestroy: "+getPagerPosition());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d("tang", "onDestroyView: "+getPagerPosition());

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //第二个参数container表示Fragment父容器，用来决定Fragment的宽高（如果Fragment设置了match_parent的话），
        //第三个参数为true表示将该fragment放入到container中，为false表示先不放入container。
        View view = inflater.inflate(R.layout.fragment_dynamic, container, false);
        Bundle arguments = getArguments();
        if (arguments != null) {
            ImageView iv_phone = view.findViewById(R.id.iv_phone);
            TextView tv_desc = view.findViewById(R.id.tv_desc);
            iv_phone.setImageResource(arguments.getInt("image_id", R.drawable.huawei));
            tv_desc.setText(arguments.getString("desc"));
        }
        return view;
    }
}